/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.palette;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.tools.CreationTool;
import org.fireflow2.designer.eclipse.commands.AbstractCreateNodeCommand;
import org.fireflow2.designer.eclipse.modelwrapper.Wrapper;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public class MyCreationTool extends CreationTool {
	protected void performCreation(int button) {	
		executeCurrentCommand();
	}
	protected void executeCurrentCommand() {
		Command curCommand = getCurrentCommand();
		if (curCommand != null && curCommand.canExecute())
			executeCommand(curCommand);
		setCurrentCommand(null);
		
		Wrapper model = null;
		if (curCommand instanceof AbstractCreateNodeCommand){
			model = ((AbstractCreateNodeCommand)curCommand).getNewModelWrapper();
		}
		EditPartViewer viewer = getCurrentViewer();
		selectAddedObject(viewer,model);
	}

	/*
	 * Add the newly created object to the viewer's selected objects.
	 */
	private void selectAddedObject(EditPartViewer viewer,Object model) {
			if (model == null || viewer == null)
				return;
			Object editpart = viewer.getEditPartRegistry().get(model);
			if (editpart instanceof EditPart) {
				// Force the new object to get positioned in the viewer.
				viewer.flush();
				viewer.select((EditPart) editpart);
			}
	}
}
